package com.game.sdk.pay.wftpay;

import android.text.TextUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.game.sdk.YTAppService;
import com.game.sdk.domain.ResultCode;
import com.game.sdk.init.NetCallBack;
import com.game.sdk.ui.ChargeActivity;
import com.game.sdk.util.ActivityTaskManager;
import com.game.sdk.util.Constants;
import com.game.sdk.util.DialogUtil;
import com.game.sdk.util.JsonUtil;
import com.game.sdk.util.MResource;
import com.game.sdk.util.Util;
import com.switfpass.pay.MainApplication;
import com.switfpass.pay.activity.PayPlugin;
import com.switfpass.pay.bean.RequestMsg;

public class WFTPayActivity extends Activity {
	String TAG = "IPayNowActivity";

	int tag = 0;
	private double charge_money;// 需要充值的金额
	private String serverid;// 充值的服务器id；
	private String productname;// 充值游戏名称
	private String productdesc;// 产品描述
	private String fcallbackurl;// 充值回调地址，由游戏方传递
	private String roleid;// 角色id；
	private String attach;// 游戏方传递的拓展参数
	private long lasttime;// 防止用户点击刷新
	private String sign;

	private Context atx;

	private String pay_id;
	private String OrderNo;
	private String pay_token;
	private String token_id;

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	boolean isTag = false;

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onCreate(Bundle intent) {
		super.onCreate(intent);
		setContentView(MResource.getIdByName(this, Constants.Resouce.LAYOUT,
				"wft_pay_layout"));
		this.atx = this;
		Intent data = getIntent();
		roleid = data.getStringExtra("roleid");
		serverid = data.getStringExtra("serverid");
		charge_money = data.getDoubleExtra("money", 0);
		productname = data.getStringExtra("productname");
		productdesc = data.getStringExtra("productdesc");
		fcallbackurl = data.getStringExtra("fcallbackurl");
		attach = data.getStringExtra("attach");

		pay_id = data.getStringExtra("pay_id");
		OrderNo = data.getStringExtra("order_id");
		pay_token = data.getStringExtra("pay_token");

		pay();
	}

	/**
	 * 发起支付
	 * */
	private void pay() {
		payByHuoService();
	}

	private void payByHuoService() {

		DialogUtil.showDialog(this, "正在努力的加载...");
		NetUtil.reqWftpay(YTAppService.userinfo.mem_id,
				YTAppService.userinfo.user_token,charge_money+"", (ChargeActivity.finalMoney>0?ChargeActivity.finalMoney:charge_money) + "", pay_token,
				atx, new NetCallBack() {

					@Override
					public void onInitSuccess(ResultCode result) {
						String data = result.data;
						if (!TextUtils.isEmpty(data)) {
							JSONObject o;
							try {
								o = new JSONObject(data);
								// OrderNo = JsonUtil.isNull(o, "a") ? "" : o
								// .getString("a");
								token_id = JsonUtil.isNull(o, "b") ? "" : o
										.getString("b");

								if (OrderNo != null && !"".equals(OrderNo)
										&& token_id != null
										&& !"".equals(token_id)) {
									if (!Util.isWeixinAvilible(atx)) {
										// Toast.makeText(atx, "没有安装好微信，亲",
										// 0).show();
										Util.onPayReqFail("没有安装好微信，亲",
												charge_money,
												WFTPayActivity.this);
										return;
									}
									RequestMsg msg = new RequestMsg();
									double money = 0;
									if(ChargeActivity.finalMoney>=0.01){
										money = ChargeActivity.finalMoney;
									}else{
										money = charge_money;
									}
									msg.setMoney(money);
									msg.setTokenId(token_id);
									msg.setOutTradeNo(payOrderNo);
									// 微信wap支付
									msg.setTradeType(MainApplication.PAY_WX_WAP);
									PayPlugin.unifiedH5Pay(WFTPayActivity.this,
											msg);
									// WFTPayActivity.this.finish();
								} else {
									String msg = "下单失败";
									Toast.makeText(atx, msg, Toast.LENGTH_SHORT)
											.show();
								}
							} catch (JSONException e) {
								com.game.sdk.util.Util.showNetFailToast(atx,
										"请求微信支付失败", result);
								WFTPayActivity.this.finish();
								e.printStackTrace();
							}
						} else {
							com.game.sdk.util.Util.showNetFailToast(atx,
									"请求微信支付失败", result);
							WFTPayActivity.this.finish();
						}
					}

					@Override
					public void onInitFail(ResultCode result) {
						com.game.sdk.util.Util.showNetFailToast(atx,
								"请求微信支付失败", result);
						WFTPayActivity.this.finish();
					}
				});
	}

	String payOrderNo;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (data == null) {
			this.finish();
			ActivityTaskManager.getInstance().removeActivity("ChargeActivity");
			return;
		}

		String respCode = data.getExtras().getString("resultCode");
		if (!TextUtils.isEmpty(respCode)
				&& respCode.equalsIgnoreCase("success")) {
			Util.onPayReqSuc("支付成功", charge_money, WFTPayActivity.this);
		} else { // 其他状态NOPAY状态：取消支付，未支付等状态
			Util.onPayReqFail("支付失败", charge_money, WFTPayActivity.this);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
